import { Component, OnInit, ViewChild } from '@angular/core';
import { uniqueId, remove } from 'lodash';
import { DatatableComponent } from '@iux/live';

interface DemoUser {
  id: string;
  name: string;
  age: number;
  expand: boolean;
  random: number;
  disabled: boolean;
}

@Component({
  selector: 'app-demo15',
  templateUrl: './demo15.component.html',
  styles: [],
})
export class Demo15Component implements OnInit {
  data: DemoUser[];
  @ViewChild(DatatableComponent, { static: false }) lvTable: DatatableComponent;

  constructor() {}

  ngOnInit() {
    this.initData();
  }

  initData() {
    this.data = Array.from({ length: 10 }).map((item, key) => {
      return this.initUser(key);
    });
  }

  initUser(key: number) {
    return {
      id: uniqueId('uid'),
      name: `user${key}`,
      age: 10 + key,
      expand: false,
      random: parseInt(Math.random() * 100 + '', 10),
      disabled: Math.random() > 0.5,
    };
  }

  addRow() {
    this.data = [...this.data, ...[this.initUser(0)]];
  }

  deleteRow(source: DemoUser) {
    const removedRow = remove(this.data, item => item.id === source.id);
    this.data = [...this.data];
  }
}
